﻿/**
 * Created by xjchen on 2020-03-11.
 * */

#ifndef MIDDLEWARE_DEVICEREADER_H
#define MIDDLEWARE_DEVICEREADER_H

#include "BaseObject.h"

class Application;

class TaskModule;

class OutputBuffer;

class Scheduler;

class Parameters;

class DeviceReader : public BaseObject {
public:
    DeviceReader();

    ~DeviceReader();

    virtual int64_t onRead(char *buffer, int64_t len) = 0;

    /**
     * 是否丢弃数据(子类重载该方法)，如果丢弃返回true, 将重新onRead。
     * overflowBuffers，已经写完数据但还未开始读取的buffer数目。
     * */
    virtual bool onDiscardDataOrNot(const char *data, int64_t dataLen, int overflowBuffers);

    /** 可以在此建立连接 */
    virtual void setupConnection();

    FLOW_API void setFeedback(bool isFeedback);

    FLOW_API void addTaskModule(TaskModule *pModule, int thatIndex, int64_t dataLen);

    /** 默认是双buffer(乒乓策略),可以设置更大的buffer池用于缓存。*/
    FLOW_API void setCacheBufferPool(int numBuffers);

    /**
     * 将任务绑定在某个CPU核上运行
     * 对于NUMA设备，请在创建对象后立即调用此API，因为在其他API中实现的NUMA相关内存分配策略
     * 依据为此处设定的CPU核
     *
     * @param whichCore 绑定的CPU核
     */
    FLOW_API void bindCPUCore(int coreId);

    virtual void readDataContinuously();

    virtual void start();

    virtual void stop();

    OutputBuffer *outputBuffer = 0;
    std::string uuid;

protected:
    int64_t sequence = 0L;

private:
    friend class Scheduler;

    friend class Parameters;

    int cacheBufferCount = 2;
    bool isFeedback = false;
    int64_t consumeSequence = 0;

    int paramIndex = 0;
};


#endif /** MIDDLEWARE_DEVICEREADER_H */
